package com.youkeda.meetingroom.controller;

import com.youkeda.meetingroom.model.LoginParam;
import com.youkeda.meetingroom.model.Result;
import com.youkeda.meetingroom.model.User;
import com.youkeda.meetingroom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@RestController
public class UserController {

    @Autowired
    private UserService userService;
    /**
     * 登录
     * @param loginParam
     * @return
     */
    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public Result login(@RequestBody LoginParam loginParam, HttpSession session, HttpServletResponse response) {
        Result result = new Result();
        if(loginParam.getEmail()!=null && loginParam.getPwd()!=null) {
            result = userService.login(loginParam.getEmail(), loginParam.getPwd());
            User user = (User) result.getData();
            session.setAttribute("userId",user.getId());
        }else{
            result.setSuccess(false);
        }
        return result;
    }
    /**
     * 注册
     * @param user
     * @return
     */
    @RequestMapping(value = "/user/register", method = RequestMethod.POST)
    public Result register(@RequestBody User user) {
        Result result = new Result();
        if(user != null) {
            result = userService.register(user);
        }else{
            result.setSuccess(false);
        }
        return result;
    }

}
